import './public-path'
import { createApp } from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
import routes from './router'
import App from './App.vue'


let app :any = null
let router :any= null
let history :any= null
// 将渲染操作放入 mount 函数
function mount () {
    history = createWebHashHistory((window as any).__MICRO_APP_BASE_ROUTE__ || '/vue3/')
    router = createRouter({
        history,
        routes,
    })
    app = createApp(App)
    app.use(router)
    app.mount('#app2')
    console.log('微应用child-vue3渲染了')
}

// 将卸载操作放入 unmount 函数
function unmount () {
    app?.unmount()
    history?.destroy()
    app = null
    router = null
    history = null
    console.log('微应用child-vue3卸载了')
}

// 微前端环境下，注册mount和unmount方法
if ((window as any).__MICRO_APP_ENVIRONMENT__) {
    (window as any)[`micro-app-${(window as any).__MICRO_APP_NAME__}`] = { mount, unmount }
} else {
    // 非微前端环境直接渲染
    mount()
}


declare global {
    interface Window { microApp: any }
}

window.microApp = window.microApp || {};
